import baseurl from '../common/common.js'
class Utils {
	constructor() {
		this.BASE_URL = baseurl
	}
	// 页面返回
	pageBack() {
		uni.navigateBack()
	}
	// 普通页面跳转
	navTo(url, data = {}) {
		// let pages = getCurrentPages()
		// let route = pages[pages.length - 1].route;
		let pageList = [
			'/pages/login/agreement',
			'/pages/index/message',
			'/pages/cate/goods_detail',
			'/pages/mine/settings',
			'/pages/mine/aboutus',
			'/pages/index/help_fetch'
		]
		if (pageList.indexOf(url) < 0) {
			let token = uni.getStorageSync('token')
			if(!token) return uni.navigateTo({
				url: '/pages/login/login'
			})
		}
		if(Object.keys(data).length > 0) {
			let params = ''
			let keyArr = Object.keys(data)
			let valueArr = Object.values(data)
			keyArr.forEach((item, index) => {
				let param = ''
				if(index == 0) {
					param = '?' + item + '=' + valueArr[index]
				}else {
					param = '&' + item + '=' + valueArr[index]
				}
				params += param
			})
			uni.navigateTo({
				url: url + params
			})
		}else {
			uni.navigateTo({
				url: url
			})
		}
	}
	// /upload转成http
	transHttp(url) {
		return this.BASE_URL + url
	}
	// 时间转时间戳
	transTimeStemp(ymdhis) {
		if(!ymdhis) return console.log('%c 未传入"年-月-日 时:分"格式的时间 ', 'color: #ffffff; background: #3c9cff; padding:5px 0; border-radius: 5px;')
		let ymdhisArr = ymdhis.trim().split(/[- :]/)
		let ymdhisDate = new Date(ymdhisArr[0], ymdhisArr[1], ymdhisArr[2], ymdhisArr[3], ymdhisArr[4]).getTime()
		return ymdhisDate
	}
	// 时间戳转时间
	transYmd(timestamp) {
		let date = new Date(timestamp)
		let year = date.getFullYear()
		let month = date.getMonth() + 1
		let day = date.getDay()
		let hours = date.getHours()
		let minutes = date.getMinutes()
		let seconds = date.getSeconds()
		month = month < 10 ? '0' + month : month
		day = day < 10 ? '0' + day : day
		hours = hours < 10 ? '0' + hours : hours
		minutes = minutes < 10 ? '0' + minutes : minutes
		seconds = seconds < 10 ? '0' + seconds : seconds
		return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
	}
	getTime() {
		let date = new Date(),
		h = date.getHours(),
		i = date.getMinutes();
		return (h < 10 ? "0" + h : h) + ":" + (i < 10 ? "0" + i : i)
	}
	telReg(tel) {
		return /^1(3|4|5|6|7|8|9)\d{9}$/.test(tel)
	}
	/* 手机号加密 */
	encryptTel(tel) {
		return tel.replace(/^(\d{3})\d{4}(\d{4})$/,"$1***$2")
	}
	// 页面滚动，背景透明度
	// :style="{background:'rgba(255,255,255,'+ opacity +')'}"
	// :style="{background:`linear-gradient( 91deg, rgba(255,139,209,${opacity}) 0%, rgba(255,75,99,${opacity}) 100%)`}"
	opacityCompute(scrollTop, distance = 100) {
		const top = uni.upx2px(distance)
		return scrollTop / top > 1 ? 1 : (scrollTop / top).toFixed(2)
	}
	menuButtonTop() {
		return uni.getMenuButtonBoundingClientRect().top + 'px'
	}
	menuButtonHeight() {
		return uni.getMenuButtonBoundingClientRect().height + 'px'
	}
	menuButtonPlace() {
		return uni.getMenuButtonBoundingClientRect().top + uni.getMenuButtonBoundingClientRect().height + 'px'
	}
	// 预览图片
	previewImage(urls, current) {
		uni.previewImage({
			urls: urls,
			current: current
		})
	}
}
const utilFunc = new Utils
export default utilFunc